<template>
  <j-modal
    :width="modalWidth"
    :visible="visible"
    :title="title"
    switchFullscreen
    :fullscreen="true"
    @ok="handleSubmit"
    @cancel="close"
    style="top: 50px"
    cancelText="关闭"
  >
    <a-card :bordered="false">
      <j-vxe-table
        keep-source
        ref="processTable"
        :loading="tslProWorkshopPlanProcessTable.loading"
        :columns="tslProWorkshopPlanProcessTable.columns"
        :dataSource="tslProWorkshopPlanProcessTable.dataSource"
        :maxHeight="300"
        :rowNumber="true"
        :rowSelection="true"
        :rowSelectionType="checkval"
        @selectRowChange="handleSelectRowChange"
        :toolbar="false"
      >
      </j-vxe-table>
    </a-card>
  </j-modal>
</template>
<script>
import '@/assets/less/TableExpand.less'
import { getAction } from '@api/manage'
import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
import { JVXETypes } from '@/components/jeecg/JVxeTable'

export default {
  name: 'WorkshopPlanProcessModal',
  mixins: [JVxeTableModelMixin],
  components: {},
  // props: ['category', "modalWidth",],
  props: {
    category: {
      type: String,
    },
    modalWidth: {
      type: Number,
      required: false,
      default: 1250,
    },
    checkval: {
      type: String,
      default: 'checkbox',
    },
    supplierId: {
      type: String,
      required: false,
    },
  },
  data() {
    return {
      description: '原料领料单-车间加工计划',
      visible: false,
      title: '原料领料单-车间加工计划',
      // 车间计划安排加工
      tslProWorkshopPlanProcessTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '车间计划安排编号',
            key: 'billCode',
            type: JVXETypes.normal,
            minWidth: '200px',
            sortable: true,
            disabled: true,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '配方号',
            key: 'formulaNo',
            type: JVXETypes.normal,
            minWidth: '200px',
            sortable: true,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '配方编码',
            key: 'formulaCode',
            type: JVXETypes.normal,
            minWidth: '200px',
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '配方名称',
            key: 'formulaName',
            type: JVXETypes.normal,
            minWidth: '200px',
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '生产锅',
            key: 'kettleId',
            type: JVXETypes.select,
            disabled:true,
            dictCode: 'tsl_md_kettle,kettle_name,kettle_no',
            minWidth: '200px',
            placeholder: '请选择${title}',
            defaultValue: '',
          },
          {
            title: '锅数',
            key: 'kettleQuantity',
            type: JVXETypes.normal,
            width: '100px',
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '已领料锅数',
            key: 'pickedKettleQuantity',
            type: JVXETypes.normal,
            minWidth: '100px',
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '本次领料锅数',
            key: 'pickKettleQuantity',
            type: JVXETypes.inputNumber,
            minWidth: '200px',
            placeholder: '请输入${title}',
            defaultValue: 0,
            validateRules: [{ required: true, message: '${title}不能为空' },
            { handler: this.validateQuantityHandler },
            ],
          },
        ],
      },
      selectedRows:[],
      url: {
        tslProWorkshopPlanProcess: {
          list: '/pro/tslProWorkshopPlan/queryTslProWorkshopPlanProcessByMainId',
        },
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    handleSelectRowChange(event) {
      let { row, action, selectedRows, $table } = event
      this.selectedRows = selectedRows
      console.log(this.selectedRows)
    },
    validateQuantityHandler(type, value, row, column, callback, target) {
        if (type === 'blur' || type === 'getValues') {
          try {
            if(value<=0){
                callback(false, "锅数必须大于0")
            }else{
                callback(true)
            }
          } catch (e) {
            callback(false, e)
          }
        } else {
          callback(true) // 不填写或者填写 null 代表不进行任何操作
        }
      },
    handleSubmit() {
      this.getSelectRows()
      this.close()
    },
    getSelectRows(){
        let selectedRow = null;
      if(this.checkval=='radio'){
         selectedRow = this.selectedRows[0];
      }else{
         selectedRow = this.selectedRows
      }
      //传递选中的数据
      console.log(selectedRow,"传递选中的数据")
      this.$emit("handleOk", selectedRow);
    },
    add(arg) {
      this.showModal(arg)
    },

    showModal(arg) {
      this.tslProWorkshopPlanProcessTable.dataSource = []
      this.visible = true
      this.loadData(arg)
    },
    initDictConfig() {},
    close() {
      this.visible = false
    },
    loadData(arg) {
      if (!this.url.tslProWorkshopPlanProcess.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      let params = { id: arg }
      this.tslProWorkshopPlanProcessTable.loading = true
      getAction(this.url.tslProWorkshopPlanProcess.list, params)
        .then((res) => {
          if (res.success) {
            console.log("车间计划安排加工记录",res);
            this.tslProWorkshopPlanProcessTable.dataSource = res.result.map(item=>{
                item.pickKettleQuantity=item.kettleQuantity-item.pickedKettleQuantity
                return item
            });

          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.tslProWorkshopPlanProcessTable.loading = false
        })
    },

  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
